/*
 *  Copyright (C) 2009 takaji
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * InitialSetupView.java
 *
 * Created on Dec 26, 2009, 10:06:00 PM
 */
package dakside.hacc.modules.system.initialsetup;

import org.dakside.utils.DateTimeHelper;
import org.dakside.utils.SystemHelper;
import org.jdesktop.application.Action;

/**
 *
 * @author Takaji
 */
public class InitialSetupView extends javax.swing.JPanel {

    private InitialSetup controller = null;

    /** Creates new form InitialSetupView */
    public InitialSetupView() {
        initComponents();
        controller = new InitialSetup(this);
        txtYear.setText(String.valueOf(DateTimeHelper.thisYear()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        mnuClear = new javax.swing.JMenuItem();
        btnCreateAccountTree = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtStatus = new javax.swing.JTextArea();
        btnCreateAccountPeriod = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtYear = new javax.swing.JTextField();
        btnCreateTransactionType = new javax.swing.JButton();
        btnCreateCurrencies = new javax.swing.JButton();

        jPopupMenu1.add(mnuClear);

        btnCreateAccountTree.setText("Create default Account Tree");
        btnCreateAccountTree.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateAccountTreeActionPerformed(evt);
            }
        });

        txtStatus.setColumns(20);
        txtStatus.setRows(5);
        txtStatus.setComponentPopupMenu(jPopupMenu1);
        jScrollPane1.setViewportView(txtStatus);

        btnCreateAccountPeriod.setText("Create Account Period for specified year");
        btnCreateAccountPeriod.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateAccountPeriodActionPerformed(evt);
            }
        });

        jLabel1.setText("Year");

        txtYear.setText("2009");

        btnCreateTransactionType.setText("Create Transaction Types");
        btnCreateTransactionType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateTransactionTypeActionPerformed(evt);
            }
        });

        btnCreateCurrencies.setText("Create currencies");
        btnCreateCurrencies.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateCurrenciesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
                    .addComponent(btnCreateAccountTree, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
                    .addComponent(btnCreateCurrencies, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(6, 6, 6)
                        .addComponent(txtYear, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCreateAccountPeriod, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE))
                    .addComponent(btnCreateTransactionType, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnCreateCurrencies)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCreateAccountTree)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCreateAccountPeriod))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCreateTransactionType)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnCreateAccountTreeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateAccountTreeActionPerformed
        //create accounting tree
        controller.createSampleAccountTree();
    }//GEN-LAST:event_btnCreateAccountTreeActionPerformed

    private void btnCreateAccountPeriodActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateAccountPeriodActionPerformed
        try {
            int year = Integer.parseInt(txtYear.getText());
            if (year < 2000 || year > 2100) {
                log("Year should >= 2000 and < 2100.");
            } else {
                controller.createAccountPeriod(year);
            }
        } catch (Exception ex) {
            log("Invalid year");
        }
    }//GEN-LAST:event_btnCreateAccountPeriodActionPerformed

    private void btnCreateTransactionTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateTransactionTypeActionPerformed
        //controller.createSampleTransactionTypes();
        //added by michael
        controller.createTransactionTypes();
    }//GEN-LAST:event_btnCreateTransactionTypeActionPerformed

    private void btnCreateCurrenciesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateCurrenciesActionPerformed
        controller.createCurrencies();
    }//GEN-LAST:event_btnCreateCurrenciesActionPerformed

    void log(String message) {
        txtStatus.append(message);
        txtStatus.append(SystemHelper.getLineSeparator());
    }

    @Action
    public void clearLog() {
        txtStatus.setText("");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCreateAccountPeriod;
    private javax.swing.JButton btnCreateAccountTree;
    private javax.swing.JButton btnCreateCurrencies;
    private javax.swing.JButton btnCreateTransactionType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenuItem mnuClear;
    private javax.swing.JTextArea txtStatus;
    private javax.swing.JTextField txtYear;
    // End of variables declaration//GEN-END:variables
}
